#include <iostream>

#include "LogicServerLogin.h"
#include "LogicServerManager.h"
#include "ProtoSockBase.h"


using namespace std;

LogicServerManager * LogicServerManager::pclassobj = NULL;

LogicServerManager * LogicServerManager::GetInstance()
{
	if (NULL == pclassobj)
	{
		pclassobj = new LogicServerManager;

		pclassobj->RegProto();
	}

	return pclassobj;
}

int LogicServerManager::RegProto()
{
	//注册登录请求消息的回调
	_dispatch.Register<Base::LoginReq>(ProtoManager::ProcessProto<Base::LoginReq, Base::LoginResp, LogicLoginManager>);
	_dispatch.Register<Base::HitBrickReq>(ProtoManager::ProcessProto<Base::HitBrickReq, Base::HitBrickResp, LogicLoginManager>);
	_dispatch.Register<Base::BallDeadReq>(ProtoManager::ProcessProto<Base::BallDeadReq, Base::BallDeadResp, LogicLoginManager>);

	return 0;
}

int LogicServerManager::OnMessage(Message * msg, void * param)
{
	int ret = _dispatch.Dispath(msg, param);
	unsigned fd = *(int *)param;

	if (ret)
	{
		//返回报错
		Base::ErrRetReq * msg = new Base::ErrRetReq;
		msg->set_msg("invalid req msg");

		ProtoSockBase::SendProto(fd, msg);

		return ret;
	}

	if (fd_msg.count(fd) && fd_msg[fd] != NULL)
	{
		//发送响应的消息
		ProtoSockBase::SendProto(fd, fd_msg[fd]);

		//处理完之后，把消息指针置空
		fd_msg[fd] = NULL;
	}

	return 0;
}
